<?php

/**
 * 2D Rectangle
 * 
 *            Top
 *      ╔═════════════╗
 *      ║             ║
 * Left ║             ║ Right
 *      ║             ║
 *      ╚═════════════╝
 *           Bottom
 *
 * @author David Safranek <safrad@rosada.cz>
 */
class SxRect {
	
	/**
	 *
	 * @var float
	 */
	private $left; // x1
	
	/**
	 *
	 * @var float
	 */
	private $top; // y1

	/**
	 *
	 * @var float
	 */
	private $right; // x2
	
	/**
	 *
	 * @var float
	 */
	private $bottom; // y2
	
	/**
	 * 
	 * @param number $left
	 * @param number $top
	 * @param number $right
	 * @param number $bottom
	 */
	public function __construct($left, $top, $right, $bottom) {
		$this->left = $left;
		$this->top = $top;
		$this->right = $right;
		$this->bottom = $bottom;
	}

	/**
	 * 
	 * @return float
	 */
	public function getLeft() {
		return $this->left;
	}

	/**
	 * 
	 * @return float
	 */
	public function getTop() {
		return $this->top;
	}

	/**
	 * 
	 * @return float
	 */
	public function getRight() {
		return $this->right;
	}

	/**
	 * 
	 * @return float
	 */
	public function getBottom() {
		return $this->bottom;
	}

	/**
	 * 
	 * @param float $left
	 * @return SxRect
	 */
	public function setLeft($left) {
		$this->left = $left;
		return $this;
	}

	/**
	 * 
	 * @param float $top
	 * @return SxRect
	 */
	public function setTop($top) {
		$this->top = $top;
		return $this;
	}

	/**
	 * 
	 * @param float $right
	 * @return SxRect
	 */
	public function setRight($right) {
		$this->right = $right;
		return $this;
	}

	/**
	 * 
	 * @param float $bottom
	 * @return SxRect
	 */
	public function setBottom($bottom) {
		$this->bottom = $bottom;
		return $this;
	}

		/**
	 * 
	 * @return float
	 */
	public function getWidth() {
		return abs($this->getRight() - $this->getLeft());
	}

	/**
	 * 
	 * @return float
	 */
	public function getHeight() {
		return abs($this->getBottom() - $this->getTop());
	}

	/**
	 * Aspect ratio
	 * @return float
	 */
	public function getRatio() {
		return $this->getWidth() / $this->getHeight();
	}

	/**
	 * 
	 * @param float $ratio
	 * @return SxRect
	 */
	public function touchInside($ratio) {
		$thisRatio = $this->getRatio();
		if ($ratio == $thisRatio) {
			// Use whole rectangle
			return $this;
		} else if ($ratio > $thisRatio) {
	/*              Top
	 *      ╔════════════════╗
	 *      ╟────────────────╢
	 * Left ║     result     ║ Right
	 *      ╟────────────────╢
	 *      ╚════════════════╝
	 *            Bottom
	 */
			$newHeight = $this->getHeight() * $this->getRatio() / $ratio;
			$newTop = $this->getTop() + ($this->getHeight() - $newHeight) / 2;
			return new SxRect($this->getLeft(), $newTop, $this->getRight(), $newTop + $newHeight);
		} else { // if ($ratio < $thisRatio) {
	/*              Top
	 *      ╔═══╤═══════╤═══╗
	 *      ║   │       │   ║
	 * Left ║   │result │   ║ Right
	 *      ║   │       │   ║
	 *      ╚═══╧═══════╧═══╝
	 *            Bottom
	 */
			$newWidth = $this->getWidth() * $ratio / $this->getRatio();
			$newLeft = $this->getLeft() + ($this->getWidth() - $newWidth) / 2;
			return new SxRect($newLeft, $this->getTop(), $newLeft + $newWidth, $this->getBottom());
		}
	}
}
